package com.dbs;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.ContactsContract.DeletedContacts;

public class DataHelper {
	private Context context;
	private MySQLiteHelper mhelper;

	public DataHelper(Context context) {
		super();
		this.context = context;
		mhelper = new MySQLiteHelper(context);
	}
	public void insert(String contents, String name, String pager) {
		String query = query(pager, name);
		if(query==null){
			String sql = "insert into News (contents,name,pager) values(?,?,?)";
			SQLiteDatabase db = mhelper.getWritableDatabase();
			db.execSQL(sql, new Object[] { contents, name, pager });
			db.close();
			
		}else{
			if(query!=contents){
				delete(pager,name);
				String sql = "insert into News (contents,name,pager) values(?,?,?)";
				SQLiteDatabase db = mhelper.getWritableDatabase();
				db.execSQL(sql, new Object[] { contents, name, pager });
				db.close();
			}
		}
			
		}	
	public String query(String pager, String name) {
		String sql = "select contents from News where pager=? and name=?";
		SQLiteDatabase db1 = mhelper.getReadableDatabase();
		
		Cursor cursor = db1.rawQuery(sql, new String[]{pager,name});	
		String contents = null;
		int id=0;
		while (cursor.moveToNext()) {
			contents = cursor.getString(cursor.getColumnIndex("contents"));	
			
		}
		cursor.close();
		db1.close();
		return contents;		
	}
	public void delete(String pager,String name){
		String sql="delete from News where pager=? and name=?";
		SQLiteDatabase rdb = mhelper.getWritableDatabase();
		rdb.execSQL(sql, new Object[]{pager,name});
		rdb.close();
	}
}
